package com.lovo.invoicing.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lovo.invoicing.bean.PageBean;
import com.lovo.invoicing.bean.RoleBean;
import com.lovo.invoicing.bean.RoleLimit;
import com.lovo.invoicing.bean.LimitBean;
import com.lovo.invoicing.mapper.LimitMapper;
import com.lovo.invoicing.mapper.RoleMapper;
import com.lovo.invoicing.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, RoleBean> implements IRoleService {
    @Autowired
    RoleMapper roleMapper;
    @Autowired
    LimitMapper limitMapper;

    @Override
    public IPage<RoleBean> queryRolesByPage(PageBean<RoleBean> pageBean) {

        return roleMapper.selectPage(new Page(pageBean.getPageNo(), pageBean.getPageSize()),null);
    }

    @Override
    public List<RoleBean> queryRoles() {
        return baseMapper.selectList(null);
    }

    @Override
    @Transactional
    public void addRole(String roleName, int[] limitIds) {
        RoleBean roleBean = new RoleBean();
        roleBean.setRoleName(roleName);
        roleMapper.insert(roleBean);
        List<RoleLimit> list = new ArrayList<>();
        for (int limitId : limitIds) {
            RoleLimit rl =new RoleLimit();
            rl.setRoleId(roleBean.getRoleId());
            rl.setLimitId(limitId);
            list.add(rl);
        }
        roleMapper.insertRoleLimit(list);
    }

    @Override
    @Transactional
    public void deleteRoleById(int roleId) {
         roleMapper.deleteRoleLimitByRoleId(roleId);
         roleMapper.deleteById(roleId);
    }

    @Override
    public List<String> queryLimitsByRoleIds(int[] roleIds) {
        return roleMapper.selectLimitsByRoleIds(roleIds);
    }

    @Override
    @Transactional
    public void updateRoleLimit(int roleId, int[] limitIds) {
        roleMapper.deleteRoleLimitByRoleId(roleId);
        List<RoleLimit> list = new ArrayList<>();
        for (int limitId : limitIds) {
            RoleLimit rl =new RoleLimit();
            rl.setRoleId(roleId);
            rl.setLimitId(limitId);
            list.add(rl);
        }
        roleMapper.insertRoleLimit(list);
    }

    @Override
    public List<LimitBean> queryLimitBeans() {
        return limitMapper.selectList(null);
    }

    @Override
    public List<Integer> queryRoleIdsByUserId(int userId) {
        return roleMapper.selectRoleIdsByUserId(userId);
    }

    @Override
    public List<String> queryLimitDescByRoleId(int roleId) {
        return roleMapper.selectLimitDescByRoleId(roleId);
    }

    @Override
    public List<Integer> queryLimitIdsByRoleId(int roleId) {
        return roleMapper.selectLimitIdsByRoleId(roleId);
    }
}
